package com.jingan.nursinghome.regulatory.select

import android.app.Activity
import android.util.Log
import cn.android.support.v7.lib.sin.crown.base.BaseListenner
import cn.android.support.v7.lib.sin.crown.kotlin.common.Toast
import cn.android.support.v7.lib.sin.crown.kotlin.https.Https
import cn.android.support.v7.lib.sin.crown.kotlin.https.ok.Okhttp
import com.jingan.nursinghome.regulatory.http.Http
import com.jingan.nursinghome.regulatory.model.Images
import com.jingan.nursinghome.regulatory.model.Message
import com.jingan.nursinghome.regulatory.model.Qfinfo
import com.jingan.nursinghome.regulatory.model.Topic

object KHttp {
    var base = "http://120.76.45.102:8080/"
    var urlqfimgs = base + "yanglaoapp/qwadmin/index.php/Home/Qfview/qfimgs"//图片
    var img = base + "yanglaoapp/qwadmin"//图片
    var qianming = base + "yanglaoapp/qwadmin"//签名图片前缀
    var urlqfinfCae = base + "qwadmin/index.php/Home/Qfview/qfiptinfos"//查看巡检信息,签名，主要沟通人员，备注
    var urlqfinfo = base + "yanglaoapp/qwadmin/index.php/Home/Qujiduan"//区级端/查看巡检
    var urltimu = base + "yanglaoapp/qwadmin/index.php/Home/Qfview/topiclist"//题目

    var urlxianqin = base + "yanglaoapp/qwadmin/index.php/Home/Qfview/qfiptinfos"//详情

    var urlqfinfo2 = base + "yanglaoapp/qwadmin/index.php/Home/Yljgduan"//养老机构端。
    var urltimu2 = base + "yanglaoapp/qwadmin/index.php/Home/Yljginfo/topiclist"//养老机构题目

    /**
     * 获取图片信息
     *
     * @param activity
     * @param iid      巡检信息id
     */
    fun qfinfoImg(activity: Activity, iid: String, baseListenner: BaseListenner<java.util.ArrayList<Images>>?) {
        Okhttp.url(urlqfimgs).showLoad(activity).addParam("iid", iid).addParam("ismic", SelectMain.ismic).onSuccess {
            //Log.e("test","图片信息:\t"+it)
        }.post<Message<ArrayList<Images>>>() {
            it?.list?.let {
                baseListenner?.onResult(it)
            }
        }
    }

    /**
     * 获取信息信息签名，备注，主要沟通人员
     *
     * @param activity
     * @param iid      巡检信息id
     */
    fun qfinfoCase(activity: Activity, iid: String, baseListenner: BaseListenner<List<Qfinfo>>?) {
        Okhttp.url(urlqfinfCae).addParam("iid", iid).showLoad(activity).post<List<Qfinfo>>() {
            baseListenner?.onResult(it)
        }
    }


    fun qfinfo(oid: String?, starttime: String?, endtime: String?, activity: Activity?, callbak: (ArrayList<Qfinfo>) -> Unit) {
        //获取巡检信息
        var url= urlqfinfo//区级端
        if(SelectMain.utype==SelectMain.UTYPE_2){
            url=urlqfinfo2//养老机构端
        }
        Okhttp.url(url).showLoad(activity).showParams(false)
                .addParam("oid", oid).addParam("starttime", starttime).addParam("endtime", endtime).addParam("ismic", SelectMain.ismic)
                .onFailure {
                    Log.e("test", "失败:\t" + it)
                }.onSuccess {
                    //Log.e("test", "巡检数据:\t" + it)
                }.post<ArrayList<Qfinfo>>() {
                    callbak(it)
                }
    }

    fun Topic(activity: Activity?, iid: String, baseListenner: BaseListenner<ArrayList<Topic>>?) {
        var url= urltimu//区级端
        if(SelectMain.utype==SelectMain.UTYPE_2){
            url=urltimu2//养老机构端
        }
        Okhttp.url(url).showLoad(activity).addParam("iid", iid).addParam("ismic", SelectMain.ismic).onSuccess {
            //Log.e("test", "题目:\t" + it)
        }.post<Message<ArrayList<Topic>>>() {
            if (it.code.equals("0")) {
                it?.list.let {
                    //Log.e("test","题目个数:\t"+it?.size)
                    baseListenner?.onResult(it)
                }
            } else {
                Toast.show(it.errmsg)
            }
        }
    }

    fun xiangqin(activity: Activity?, iid: String, baseListenner: BaseListenner<Qfinfo>?) {
        Okhttp.url(urlxianqin).showLoad(activity).addParam("iid", iid).addParam("ismic", SelectMain.ismic).onSuccess {
            //Log.e("test", "详情:\t" + it)
        }.post<Message<ArrayList<Qfinfo>>>() {
            it?.list!![0]?.let {
                baseListenner?.onResult(it)
            }
        }
    }

}